/**
 * 
 */
package com.et114.modules.products.digitalTv.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.NumberUtils;
import com.et114.core.utility.StringPool;
import com.et114.core.utility.ThreadSynchronizer;
import com.et114.modules.products.commons.Const;
//import com.et114.modules.products.commons.Context;
import com.et114.modules.products.commons.PorJsonObj;
import com.et114.modules.products.digitalTv.action.DigitalTvForm;
import com.et114.modules.products.digitalTv.vo.DigitalTv;
import com.et114.modules.products.digitalTv.vo.DigitalTvSpeedType;
import com.et114.modules.products.digitalTv.services.DigitalTvServiceImpl;
import com.et114.modules.products.procontent.services.ProContentServiceImpl;
import com.et114.modules.products.procontent.vo.ProContent;
import com.et114.modules.products.supplier.services.SupplierServiceImpl;
import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/**
 * @author guanhw
 */
public class DigitalTvAction extends BaseDispatchAction {

	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getDigitalTv( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_DT_TYPE ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;
		
		
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		ProSuppliers psvo = new ProSuppliers( ) ; 
		psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_TV + StringPool.COMMA ) ; 
		psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) ) ;
		List< ProSuppliers > proTypes = ssbean.querySuppliersList ( psvo ) ; 
		request.setAttribute ( "proTypes" , proTypes ) ;
		//BIS
		DigitalTvServiceImpl sss = ( DigitalTvServiceImpl )getBean ( "digitalTvServiceImpl" ) ;
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl ) getBean ( "proContentServiceImpl" );

		
		DigitalTvForm digitalTvForm = ( DigitalTvForm ) form ; 
		DigitalTv digitalTv = new DigitalTv( ) ; 
		PropertyUtils.copyProperties ( digitalTv , digitalTvForm ) ; 
		request.setAttribute ( "searchVo" , digitalTv ) ; 
		
		Long sdid = digitalTvForm.getProId ( ) ; 
		if ( sdid != null && sdid.longValue ( ) != 0  ) { // load one
			digitalTv = sss.getDigitalTvById ( sdid ) ; 
			request.setAttribute ( "digitalTv" , digitalTv ) ;
			
			//
			ProContent pcontent = proContentServiceImpl
								.getProContent ( new ProContent ( Byte
										.valueOf ( Const.PRO_TYPE_TV ) , sdid ) );
			if ( pcontent != null )
				request.setAttribute ( "content" , pcontent );
			
			
			return mapping.findForward ( "digitalTv" ) ; 
		}
		else { //loads
			DataList dl = sss.getDigitalTvs (  digitalTv ) ;
			List< DigitalTv > dts = dl.getNowlist ( ) ; 
			DigitalTvSpeedType digitalTvSpeedType = null ; 
			for ( DigitalTv tvo : dts ) {
				tvo.setDigitalTvSpeedType ( null ) ; 
				for ( BizOption bioCt : bssOps ) {
					if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
							tvo.getdSpeedTypeTypeId ( ) ) ) {
						digitalTvSpeedType = new DigitalTvSpeedType( ) ;
						digitalTvSpeedType.setdSpeedTypeTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
						digitalTvSpeedType.setDigitalTvTypeName ( bioCt.getOptionName ( ) ) ; 
						tvo.setDigitalTvSpeedType ( digitalTvSpeedType ) ; 
						break ; 
					}
				}
			}
			
			request.setAttribute ( "dl" , dl ) ; 
			return mapping.findForward ( "digitalTvs" ) ; 
		}
		
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward toEditDigitalTv( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_DT_TYPE ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;
		
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		ProSuppliers psvo = new ProSuppliers( ) ; 
		psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) ) ;
		psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_TV + StringPool.COMMA ) ; 
		List< ProSuppliers > proTypes = ssbean.querySuppliersList ( psvo ) ; 
		request.setAttribute ( "proTypes" , proTypes ) ;
		
		return mapping.findForward ( "digitalTv" ) ; 
	}
	
	
	
	public  ActionForward editDigitalTv( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		DigitalTvForm digitalTvForm = ( DigitalTvForm ) form ; 
		//DIC
		
		//PARAMS 
		
		//BIS
		DigitalTvServiceImpl sss = ( DigitalTvServiceImpl )getBean ( "digitalTvServiceImpl" ) ;
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl ) getBean ( "proContentServiceImpl" );
		
		
		DigitalTv digitalTv = new DigitalTv( ) ; 
		PropertyUtils.copyProperties ( digitalTv , digitalTvForm ) ; 
		
		digitalTv.setUpdtUser (  getCurrentUser ( request ).getUserId ( ) ) ;
		Long id = digitalTvForm.getProId ( ) ; 
		
		Byte actStatus = digitalTvForm.getProActStatus ( ) ; 
		if (actStatus != null ) {
			if ( Const.PRO_ACTION_SUB == actStatus ) {
				if ( NumberUtils.isEmptyOrZero ( id ) )
					digitalTv.setCrtUser ( getCurrentUser ( request )
							.getUserId ( ) );
				id = saveAndUpdateDigitalTv ( digitalTv , digitalTvForm
						.getDtImageff ( ) , sss , request );
			}
			else if ( Const.PRO_ACTION_PUB == actStatus ) {
				digitalTv = sss.getDigitalTvById ( id ) ; 
				digitalTv.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED ) ; 
				id = saveAndUpdateDigitalTv( digitalTv , digitalTvForm.getDtImageff ( ) , sss , request ) ; 
			}

			else if ( Const.PRO_ACTION_SUB_CHECK == actStatus ) {
				digitalTv = sss.getDigitalTvById ( id ) ; 
				Date offTime = digitalTvForm.getOffTime ( ) ; 
				if (  ( offTime != null && ( new Date ( )
								.getTime ( )- offTime.getTime ( ) >= 0 ) ) ) 			
					return PorJsonObj.response ( response , PorJsonObj.error ( "offTime" ) );				

				return PorJsonObj.response ( response , PorJsonObj.success (  "!" ) );	
			}
			
			else if ( Const.PRO_ACTION_SUB_PUB == actStatus ) {
				digitalTv.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED );
				if ( id != null && id.longValue ( ) != 0 ) // update
					id = saveAndUpdateDigitalTv( digitalTv , digitalTvForm.getDtImageff ( ) , sss , request ) ; 
			}
			
			
			else if ( Const.PRO_ACTION_YULAN == actStatus ) {
				
			}
			else if ( Const.PRO_ACTION_OUT == actStatus ) {
				//只更新 下架状态
				DigitalTv odt = sss.getDigitalTvById ( id ) ; 
				Byte bos = odt.getProOnOffStatus ( ) ; 
				if ( !NumberUtils.isEmptyOrZero ( bos ) &&  Const.PRO_PUB_STATUS_PUBED == bos.byteValue ( )  ) { 
					odt.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ; 
					sss.updateDigitalTv ( odt ) ; 						
				}
			}			
			
			//save content
			if ( Const.PRO_ACTION_SUB == actStatus || Const.PRO_ACTION_SUB_PUB == actStatus ) { 
				ProContent content = new ProContent( Byte.valueOf ( Const.PRO_TYPE_TV ) , id )  ; 
				content.setContent ( digitalTvForm.getContent ( ) ) ; 
				proContentServiceImpl.saveAndUpdateProContent ( content ) ;		
			}
		}
		//Context.indexReload ( ) ; 
		return new ActionForward( "digitalTv.go?method=getDigitalTv&proId=" + id , true ); 
	}	
	
	private Long saveAndUpdateDigitalTv( DigitalTv dtVo , FormFile formFile  , DigitalTvServiceImpl sss , HttpServletRequest request ) throws Exception {
		Long proId = dtVo.getProId ( ) ; 
		String uploadFn = formFile.getFileName ( ); 
		if ( !StringUtils.isEmpty ( uploadFn ) && !StringUtils.isBlank ( uploadFn ) )   {
			FileUploadUtil upload = new FileUploadUtil( ) ; 
			ThreadSynchronizer.threadEnter ( );
			try {
				upload.setRestrictfs ( 102400 * 5 ) ;
				String fn = upload.uploadv2 ( formFile , request , Const.PRO_DIGITALTV_IMG_UPLOAD_PATH , UUID.randomUUID ( ).toString ( ) );
				fn = Const.PRO_DIGITALTV_IMG_UPLOAD_PATH + fn;
				dtVo.setDtImage ( fn ) ; 
				//cardService.updateCard (  cardVo );				
			} finally {
				ThreadSynchronizer.threadExit ( );
			}						
		}
		
		if ( !NumberUtils.isEmptyOrZero ( proId )  ) { // update
			DigitalTv dt = sss.getDigitalTvById ( proId ) ; 
			String dtImg =  dt.getDtImage ( ) ; 
			if ( StringUtils.isEmpty ( uploadFn ) || StringUtils.isBlank ( uploadFn ) ) 
				dtVo.setDtImage ( dtImg  ) ; 
			else {
				FileUploadUtil.removeRealPathFile ( dtImg , request ) ;
			}
			sss.updateDigitalTv ( dtVo ) ;	
		}
		else { //save
			proId = sss.saveDigitalTv ( dtVo ) ; 
		}
		
		//upload

		return proId ; 
	}	
	
	public  ActionForward removeDigitalTv( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//PARAMS
		DigitalTvForm digitalTvForm = ( DigitalTvForm ) form ; 
		Byte actStatus = digitalTvForm.getProActStatus ( ) ;
		Date offTime = digitalTvForm.getOffTime ( ) ;
		//BIS
		DigitalTvServiceImpl sss = ( DigitalTvServiceImpl )getBean ( "digitalTvServiceImpl" ) ;
		
		Long[] deleteIds = digitalTvForm.getDeleteId ( ) ;
		if ( deleteIds != null ) {
			for ( Long deleteId : deleteIds ) {
				DigitalTv digitalTv = sss.getDigitalTvById (  deleteId ) ;
				Byte bos = digitalTv.getProOnOffStatus ( ) ; 
				if ( Const.PRO_ACTION_PUB == actStatus ) {
					if ( ! NumberUtils.isEmptyOrZero ( bos )
							&& Const.PRO_PUB_STATUS_PUBED != bos.byteValue ( ) ) {
						if ( offTime == null
								|| ( offTime != null && ( new Date ( )
										.getTime ( )- offTime.getTime ( ) < 0 ) ) ) {
						
							digitalTv.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED ) ; 
							digitalTv.setOffTime ( offTime ) ;
							sss.updateDigitalTv ( digitalTv ) ; 
						}
					}
				}
				else if ( Const.PRO_ACTION_OUT == actStatus ) { 
					if ( ! NumberUtils.isEmptyOrZero ( bos )
							&& Const.PRO_PUB_STATUS_PUBED == bos.byteValue ( ) ) {					
						digitalTv.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ;  
						sss.updateDigitalTv ( digitalTv ) ; 
					}
				}
				else { //delete
					/*
					digitalTv.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ; 
					digitalTv.setRemoveMark ( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ; 
					sss.updateDigitalTv ( digitalTv ) ; */
					sss.removeDigitalTv ( deleteId ) ;
				}
				
			}
		}
		//Context.indexReload ( ) ; 
		return new ActionForward( "digitalTv.go?method=getDigitalTv" , true ); 
	}	
	
	
	
	
	//4 q
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getDigitalTvs4Views( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_DT_TYPE ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;
		
		//BIS
		DigitalTvServiceImpl sss = ( DigitalTvServiceImpl )getBean ( "digitalTvServiceImpl" ) ;
		
		DigitalTvForm digitalTvForm = ( DigitalTvForm ) form ; 
		digitalTvForm.setPageSize ( 8 ) ;
		DigitalTv digitalTv = new DigitalTv( ) ; 
		PropertyUtils.copyProperties ( digitalTv , digitalTvForm ) ; 

		DataList dl = sss.getDigitalTvs4View (  digitalTv ) ;
		List< DigitalTv > dts = dl.getNowlist ( ) ; 
		DigitalTvSpeedType digitalTvSpeedType = null ; 
		for ( DigitalTv tvo : dts ) {
			tvo.setDigitalTvSpeedType ( null ) ; 
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						tvo.getdSpeedTypeTypeId ( ) ) ) {
					digitalTvSpeedType = new DigitalTvSpeedType( ) ;
					digitalTvSpeedType.setdSpeedTypeTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					digitalTvSpeedType.setDigitalTvTypeName ( bioCt.getOptionName ( ) ) ; 
					tvo.setDigitalTvSpeedType ( digitalTvSpeedType ) ; 
				}
			}
		}
		
		request.setAttribute ( "dl" , dl ) ; 
		return mapping.findForward ( "digitalTvs4View" ) ; 		
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getDigitalTv4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_DT_TYPE ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;

		
		//BIS
		DigitalTvServiceImpl sss = ( DigitalTvServiceImpl )getBean ( "digitalTvServiceImpl" ) ;
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl )getBean( "proContentServiceImpl" ) ; 		
		
		DigitalTvForm digitalTvForm = ( DigitalTvForm ) form ; 
		digitalTvForm.setPageSize (  Const.PAGE_SIZE_Q  ) ;
		DigitalTv digitalTv = new DigitalTv( ) ; 
		PropertyUtils.copyProperties ( digitalTv , digitalTvForm ) ; 
		Long proId = digitalTv.getProId ( ) ; 
		
		String flg4boc = digitalTvForm.getFlg4boc ( ) ; 
		
		boolean showModal = false ;
		if ( digitalTv.getShowModal ( ) != null
						&& digitalTv.getShowModal ( ) )
			showModal = true ; 
		
		
		if ( Const.FLG_FOR_GET_BASEINFO.equals ( flg4boc ) ) {  
			if ( NumberUtils.isEmptyOrZero(  proId) ) 
				return mapping.findForward ( "proout" );	
			if ( showModal )
				digitalTv = sss.getDigitalTvById ( proId );
			else
				digitalTv = sss.getBasetDigitalTvById4View ( proId );

			if ( digitalTv == null ) 
				return mapping.findForward ( "proout" );	
			
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						digitalTv.getdSpeedTypeTypeId ( ) ) ) {
					DigitalTvSpeedType digitalTvSpeedType = new DigitalTvSpeedType( ) ;
					digitalTvSpeedType.setdSpeedTypeTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					digitalTvSpeedType.setDigitalTvTypeName ( bioCt.getOptionName ( ) ) ; 
					digitalTv.setDigitalTvSpeedType ( digitalTvSpeedType ) ; 
					break ; 
				}
			}			

			request.setAttribute ( "vo" , digitalTv ) ;
			return mapping.findForward ( "digitalTv4View" ) ; 
		}
		else if ( Const.FLG_FOR_GET_CONTENTINFO.equals ( flg4boc ) ) {  
			ProContent pcontent = proContentServiceImpl.getProContentCacheModel (  
					new ProContent( Byte.valueOf ( Const.PRO_TYPE_TV) ,  proId  )  ) ;
			if ( pcontent != null ) 
				return PorJsonObj.response ( response , PorJsonObj
						.getProContentJSONObject ( pcontent.getContent ( ) ).toString ( ) );
			else 
				return PorJsonObj.response ( response , PorJsonObj
						.getProContentJSONObject ( "" ).toString ( ) );
		} 
		else 
			return PorJsonObj.response ( response , PorJsonObj.error (
					CONST.COUNTRY_ZH , response  , Const.AJAX_ERROR_LOAD_PRO_NOTFOUND ).toString ( ) );			

	}
	
	
}
